package com.example.mvvm.ui.my

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.example.mvvm.R
import com.example.mvvm.data.PlaceRepository
import com.example.mvvm.data.db.CoolWeatherDatabase
import com.example.mvvm.data.network.CoolWeatherNetwork
import com.example.mvvm.databinding.ActivityWetherBinding

class WetherActivity : AppCompatActivity() {
    val viewmodel by lazy {
        Log.e("wode","走了开始")
        val instance = PlaceRepository.getInstance(
            CoolWeatherDatabase().getPlaceDao(),
            CoolWeatherNetwork.getInstance()
        )

        ViewModelProvider(this@WetherActivity, MyViewModel.ViewModelFacoty(instance)).get(MyViewModel::class.java)
    }
    lateinit var binding:ActivityWetherBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_wether)
        binding=  DataBindingUtil.setContentView(this,R.layout.activity_wether)
        var p:Int = intent.getIntExtra("position",-1)

        //binding 和lifecycler要是同一个owner，要不xml数据绑定不是双向的
        binding.lifecycleOwner = this
        //需要手动将viewmodel与xml布局绑定
        binding.city = viewmodel
        lifecycleScope.launchWhenCreated {
            Log.e("wodetest",viewmodel.test)
            viewmodel.getCities(p)
        }
    }
}